package com.graduation.table.spots.controller;

import java.io.OutputStream;
import java.util.Arrays;
import java.util.Objects;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.graduation.base.Result;
import com.graduation.table.spots.entity.HotSpots;
import com.graduation.table.spots.entity.QRCodeContent;
import com.graduation.table.spots.service.IHotSpotsService;
import com.graduation.util.QRCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

 /**
 * @Description: 校内热点
 * @Author: jeecg-boot
 * @Date:   2022-05-18
 * @Version: V1.0
 */
@Slf4j
@Api(tags="校内热点")
@RestController
@RequestMapping("/hotSpots")
public class HotSpotsController {
	@Resource
	private IHotSpotsService hotSpotsService;
	
	/**
	 * 分页列表查询
	 *
	 */
	@ApiOperation(value="校内热点-分页列表查询", notes="校内热点-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(HotSpots hotSpots,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		LambdaQueryWrapper<HotSpots> queryWrapper = new LambdaQueryWrapper<>();
		Page<HotSpots> page = new Page<>(pageNo, pageSize);
		IPage<HotSpots> pageList = hotSpotsService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 * 添加
	 *
	 */
	@ApiOperation(value="校内热点-添加", notes="校内热点-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody HotSpots hotSpots) {
		hotSpotsService.save(hotSpots);
		return Result.OK("添加成功！");
	}
	
	/**
	 * 编辑
	 *
	 */
	@ApiOperation(value="校内热点-编辑", notes="校内热点-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody HotSpots hotSpots) {
		hotSpotsService.updateById(hotSpots);
		return Result.OK("编辑成功!");
	}
	
	/**
	 * 通过id删除
	 *
	 */
	@ApiOperation(value="校内热点-通过id删除", notes="校内热点-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id") String id) {
		hotSpotsService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 * 批量删除
	 *
	 */
	@ApiOperation(value="校内热点-批量删除", notes="校内热点-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids") String ids) {
		this.hotSpotsService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功！");
	}
	
	/**
	 * 通过id查询
	 *
	 */
	@ApiOperation(value="校内热点-通过id查询", notes="校内热点-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id") String id) {
		HotSpots hotSpots = hotSpotsService.getById(id);
		return Result.OK(hotSpots);
	}

	 /**
	  * 获取二维码
	  *
	  */
	 @ApiOperation(value = "获取二维码", notes = "获取二维码")
	 @GetMapping(value = "/release/code")
	 public Result<?> storeReleaseCode(@RequestParam(name = "hotSpotsId") String hotSpotsId,
									   HttpServletResponse response) throws Exception {
		 HotSpots store = hotSpotsService.getById(hotSpotsId);
		 if (Objects.isNull(store)) {
			 return Result.error("不存在该热点");
		 }
		 QRCodeContent qrCodeContent = new QRCodeContent();
		 qrCodeContent.setHotSpotsId(hotSpotsId);
		 qrCodeContent.setMessage(store.getHeader());
		 qrCodeContent.setLocation(store.getLocation());
		 qrCodeContent.setCode("hotSpots");
		 JSONObject jsonObject = (JSONObject) JSONObject.toJSON(qrCodeContent);
		 OutputStream outputStream = response.getOutputStream();
		 QRCodeUtils.encode(jsonObject.toJSONString(), outputStream);
		 return Result.OK();
	 }




}
